package com.cybertron.ironhide.spider.plugin.impl;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.eweb4j.util.CommonUtil;

import com.cybertron.ironhide.spider.SpiderListener;
import com.cybertron.ironhide.spider.plugin.TaskPoint;
import com.cybertron.ironhide.spider.plugin.util.Util;
import com.cybertron.ironhide.spider.task.Task;
import com.cybertron.ironhide.spider.xml.Site;
import com.cybertron.ironhide.spider.xml.Target;
import com.cybertron.ironhide.spider.xml.ValidHost;
import com.cybertron.ironhide.spider.xml.ValidHosts;

public class TaskPointImpl implements TaskPoint {

	private final static Logger logger = Logger.getLogger(TaskPointImpl.class);

	private Site site;

	private SpiderListener listener;

	public void init(Site site, SpiderListener listener) {
		this.site = site;
	}

	public void destroy() {
	}

	public synchronized List<Task> pushTask(List<Task> validTasks)
			throws Exception {
		List<Task> newTasks = new ArrayList<Task>();
		for (Task task : validTasks) {
			try {
				// 如果不是在给定的合法host列表里则不给于抓取
				ValidHosts vhs = task.site.getValidHosts();
				if (vhs == null || vhs.getValidHost() == null
						|| vhs.getValidHost().isEmpty()) {
					if (!CommonUtil.isSameHost(task.site.getUrl(), task.url)) {
						listener.onInfo(
								Thread.currentThread(),
								task,
								"task.url->"
										+ task.url
										+ "'s host is not the same as site.host->"
										+ task.site.getUrl());
						continue;
					}
				} else {
					boolean isOk = false;
					String taskHost = new URL(task.url).getHost();
					for (ValidHost h : vhs.getValidHost()) {
						if (taskHost.equals(h.getValue())) {
							isOk = true;
							break;
						}
					}

					if (!isOk)
						continue;
				}

				boolean isValid = true;
				try {
					if (task.seedUrl.counter.getCount() >= task.seedUrl
							.getMaxCount()) {
						logger.info("task count > maxCount in sourceUrlCheck,task count is "
								+ task.seedUrl.getMaxCount());
						isValid = false;
					}
				} catch (Exception e) {
					listener.onError(Thread.currentThread(), task, "", e);
				}

				// 如果是有效的，或者是不严格的规则那么任务都可以进入队列
				if (isValid) {
					boolean isOk = site.queue.pushTask(task);
					if (isOk) {
						newTasks.add(task);
						if (!task.isSource) {
							task.seedUrl.counter.plus();
						}
					}

				}
			} catch (Exception e) {
				continue;
			}
		}
		return newTasks;
	}

	@Override
	public List<Task> sortTasks(List<Task> tasks) throws Exception {
		float i = 0f;
		for (Task task : tasks) {
			i += 0.00001;
			// 检查url是否符合target的url规则，并且是否是来自于来源url，如果符合排序调整为0
			Target tgt = Util.matchTarget(task);
			if (tgt != null && task.isSource != null) {
				task.sort = 0
						+ CommonUtil
								.toDouble("0." + System.currentTimeMillis())
						+ i;
			} else {
				// 检查url是否符合target的sourceUrl规则，如果符合排序调整为5，否则为10
				task.sort = 10
						+ CommonUtil
								.toDouble("0." + System.currentTimeMillis())
						+ i;
			}
		}
		return tasks;
	}
}
